package com.syzg.base.role.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.syzg.common.config.SysProperties;
import com.syzg.common.controller.BaseController;
import com.syzg.common.model.WebInfo;
import com.syzg.common.util.SearchMap;
import com.syzg.common.util.MethodUtil;
import com.syzg.common.model.WebObj;
import com.syzg.common.util.PKCreator;
import com.syzg.common.exception.BaseException;
import com.syzg.core.page.PageResult;
import com.syzg.core.util.StringUtil;

import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.syzg.base.merge.model.Role;
import com.syzg.base.permission.service.PermissionService;
import com.syzg.base.role.service.RoleService;

@Controller
@RequestMapping("/path/role")
public class RoleController extends BaseController{
	
	static Logger log = LoggerFactory.getLogger(RoleController.class);
	
	@Resource
	private RoleService roleService;
	@Resource
	private PermissionService permissionService;
	
	@RequestMapping("/listRole")
	public ModelAndView listRole(WebObj webObj)throws Exception{
		ModelAndView mv = new ModelAndView("/base/jsp/role/listRole.jsp");
		SearchMap searchMap = SearchMap.forClass(Role.class);
		PageResult pageResult = roleService.findObjectsForPage(webObj.getPageNo(), SysProperties.PAGESIZE, searchMap);
        webObj.setPageResult(pageResult);
        mv.addObject("webObj",webObj);
        return mv;
	}
	
	@RequestMapping("/initRole")
	public ModelAndView initRole() throws Exception {
		ModelAndView mv = new ModelAndView("/base/jsp/role/addRole.jsp");
		try {
			mv.addObject("role", new Role());
			/*LoginObject loginObj = SystemUtil.getLoginObject();
			this.role.setCreatePersonId(new String(loginObj.getPersonId()));
			this.role.setCreator(loginObj.getPersonName());
			this.role.setCreateTime(new Date());*/
			mv.addObject("moduleList", permissionService.findAllModuleOperationForShow());
		} catch (Exception e) {
			log.error(BaseException.addExceptionLog(e));
		}
		return mv;
	}
	
	@RequestMapping("/addRole")
	public @ResponseBody String addRole(Role role,String operationId) throws Exception {
		WebObj webObj = new WebObj();
		try {
			/*LoginObject loginObj = SystemUtil.getLoginObject();
			this.role.setCreatePersonId(new String(loginObj.getPersonId()));
			this.role.setCreator(loginObj.getPersonName());
			this.role.setCreateTime(new Date());*/
			role.setId(PKCreator.getPk());
			String[] operationIds = null;
			if(StringUtil.isNotEmpty(operationId)){
				String[] strAry = operationId.split(",");
				operationIds = new String[strAry.length];
				for(int i=0;i<strAry.length;i++){
					operationIds[i] = String.valueOf(strAry[i]);
				}
			} else {
				operationIds = new String[0];
			}
			roleService.createRole(role, operationIds);
			setControllerStatus(webObj,WebInfo.STATUS_SUCCESS);
		} catch (Exception e) {
			log.error(BaseException.addExceptionLog(e));
			setControllerStatus(webObj,WebInfo.STATUS_FAILURE);
			webObj.getWebInfo().getErrorList().add("添加角色失败");
		}
		return new JSONObject(webObj).toString();
	}
	
	@RequestMapping("/viewRole")
	public ModelAndView viewRole(Role role,ModelAndView mv) throws Exception {
		mv.setViewName("/base/jsp/role/viewRole.jsp");
		try {
			String id = role.getId();
			Role roleVO = roleService.findObjectById(id);
			mv.addObject("role",roleVO);
			mv.addObject("moduleViewList",permissionService.findRoleModuleOperationForShow(id));
		} catch (Exception e) {
			log.error(BaseException.addExceptionLog(e));
		}
		return mv;
	}
	
	@RequestMapping("/editRole")
	public ModelAndView editRole(String id) throws Exception {
		ModelAndView mv = new ModelAndView("/base/jsp/role/editRole.jsp");
		try {
			Role role = roleService.findObjectById(id);
			mv.addObject("role", role);
			mv.addObject("moduleViewList",permissionService.findRoleModuleOperationForShow(id));
			WebObj webObj = new WebObj();
			mv.addObject("webObj",webObj);
		} catch (Exception e) {
			log.error(BaseException.addExceptionLog(e));
		}
		return mv;
	}
	
	@RequestMapping("/modRole")
	public @ResponseBody String modRole(String id,Role roleVO,String operationId) throws Exception {
		WebObj webObj = new WebObj();
		try {
			Role role = roleService.findObjectById(id);
			role = (Role) MethodUtil.copyProperties(role, roleVO);
			String[] operationIds = null;
			if(StringUtil.isNotEmpty(operationId)){
				String[] strAry = operationId.split(",");
				operationIds = new String[strAry.length];
				for(int i=0;i<strAry.length;i++){
					operationIds[i] = String.valueOf(strAry[i]);
				}
			} else {
				operationIds = new String[0];
			}
			
			roleService.updateRole(role, operationIds);
			setControllerStatus(webObj,WebInfo.STATUS_SUCCESS);
		} catch (Exception e) {
			log.error(BaseException.addExceptionLog(e));
			setControllerStatus(webObj,WebInfo.STATUS_FAILURE);
			webObj.getWebInfo().getErrorList().add("编辑角色失败");
		}
		return new JSONObject(webObj).toString();
	}
	
	@RequestMapping("/delRole")
	public ModelAndView delRole(String id,WebObj webObj) throws Exception {
		String[] ids = webObj.getSelectedRow();
		try {
			if(ids == null) { //删除单个
				if(id == null) {
					ids = new String[0];
				} else {
					ids = new String[]{String.valueOf(id)};
				}
			}
			String[] idsStr = new String[ids.length];
			int i = 0;
			for(String idVO: ids) {
				idsStr[i++] = new String(idVO);
			}
			roleService.deleteRole(idsStr);
		} catch (Exception e) {
			log.error(BaseException.addExceptionLog(e));
		}
		return listRole(webObj);
	}
}
